﻿using System.Collections.Generic;
using Light.Framework.Model;
using Light.Utility;
using System.Data;

namespace Light.Framework.Contract
{
    /// <summary>
    /// 组件接口
    /// </summary>
    public interface IComponent
    {
        OperateResult Delete(string id);
        OperateResult Delete(List<string> ids);

        /// <summary>
        /// 求总记录条数
        /// </summary>
        /// <returns></returns>
        int Count();

        /// <summary>
        /// 求总记录条数
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        int Count(SearchArgs args);

        /// <summary>
        /// 求总记录数
        /// </summary>
        /// <param name="tableOrViewName">数据库表或视图名</param>
        /// <param name="args"></param>
        /// <returns></returns>
        int Count(string tableOrViewName, SearchArgs args);

        /// <summary>
        /// 汇总求和
        /// </summary>
        /// <param name="columnName">数据库列名</param>
        /// <param name="args"></param>
        /// <returns></returns>
        decimal Sum(string columnName, SearchArgs args);

        /// <summary>
        /// 汇总求和
        /// </summary>
        /// <param name="tableOrViewName">数据库表或视图名</param>
        /// <param name="columnName">数据库列名</param>
        /// <param name="args"></param>
        /// <returns></returns>
        decimal Sum(string tableOrViewName, string columnName, SearchArgs args);

        /// <summary>
        /// 查询表或视图
        /// </summary>
        /// <param name="tableOrViewName"></param>
        /// <param name="searchArgs"></param>
        /// <param name="modelOrder"></param>
        /// <returns></returns>
        DataTable GetAll(string tableOrViewName, SearchArgs searchArgs, ResultOrder modelOrder);

        /// <summary>
        /// 分页查询表或视图
        /// </summary>
        /// <param name="tableOrViewName"></param>
        /// <param name="searchArgs"></param>
        /// <param name="pageArgs"></param>
        /// <param name="modelOrder"></param>
        /// <returns></returns>
        DataTable Search(string tableOrViewName, SearchArgs searchArgs, PageArgs pageArgs, ResultOrder modelOrder);
    }

    /// <summary>
    /// 组件基接口，定义基本的CRUD操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IComponent<T> : IComponent where T : class, IEntity, new()
    {
        OperateResult Create(T obj);
        OperateResult Create(List<T> objs);

        OperateResult Delete(T model);
        OperateResult Delete(List<T> models);

        OperateResult Update(T obj);
        OperateResult Update(T obj, UpdateColumn uc);
        OperateResult Update(List<T> objs);

        OperateResult Update(List<T> objs, UpdateColumn uc);

        OperateResult Save(T obj);
        OperateResult Save(List<T> models);

        /// <summary>
        /// 获取默认项，
        /// 若T实现了IDefaultable接口，则取IsDefault=true的对象
        /// 若未实现，则返回 new T();
        /// </summary>
        /// <returns></returns>
        T Get();


        /// <summary>
        ///根据唯一标识获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T Get(string id);

        /// <summary>
        ///根据唯一标识获取
        /// </summary>
        /// <param name="id"></param>
        /// <param name="resultColumn">需要查询的属性名称列表(为空时，表示查询所有)</param>
        /// <returns></returns>
        T Get(string id, ResultColumn resultColumn);
        /// <summary>
        /// 根据实体的某个属性值获取单个对象
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        //T Get(string propertyName, object value);

        /// <summary>
        /// 查询（返回符合条件的第一个对象） 
        /// </summary>
        /// <param name="args">查询参数</param>
        /// <returns></returns>
        T Get(SearchArgs args);


        /// <summary>
        /// 查询（返回符合条件的第一个对象） 
        /// </summary>
        /// <param name="args">查询参数</param>
        /// <param name="resultColumn">需要查询的属性名称列表(为空时，表示查询所有)</param>
        /// <returns></returns>
        T Get(SearchArgs args, ResultColumn resultColumn);

        /// <summary>
        ///  查询（返回符合条件的第一个对象） 
        /// </summary>
        /// <param name="args">查询参数</param>
        /// <param name="order">排序参数</param>
        /// <returns></returns>
        //T Get(SearchArgs args, ResultOrder order);

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns></returns>
        List<T> GetAll();
        List<T> GetAll(int resultCount);

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="resultColumn">需要查询的属性名称列表(为空时，表示查询所有)</param>
        /// <returns></returns>
        List<T> GetAll(ResultColumn resultColumn);

        List<T> GetAll(ResultColumn resultColumn, int resultCount);

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="args">查询参数</param>
        /// <returns></returns>
        List<T> GetAll(SearchArgs args);
        List<T> GetAll(SearchArgs args, int reslutCount);

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="args">查询参数</param>
        /// <param name="resultColumn">需要查询的属性名称列表(为空时，表示查询所有)</param>
        /// <returns></returns>
        List<T> GetAll(SearchArgs args, ResultColumn resultColumn);
        List<T> GetAll(SearchArgs args, ResultColumn resultColumn, int reslutCount);

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="args">查询参数</param>
        /// <param name="order">排序参数</param>
        /// <returns></returns>
        List<T> GetAll(SearchArgs args, ResultOrder order);
        List<T> GetAll(SearchArgs args, ResultOrder order, int reslutCount);

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="args">查询参数</param>
        /// <param name="order">排序参数</param>
        /// <param name="resultColumn">需要查询的属性名称列表(为空时，表示查询所有)</param>
        /// <returns></returns>
        List<T> GetAll(SearchArgs args, ResultOrder order, ResultColumn resultColumn);
        List<T> GetAll(SearchArgs args, ResultOrder order, ResultColumn resultColumn, int resultCount);

        /// <summary>
        ///  分页查询对象列表
        /// </summary>
        /// <param name="searchArgs">查询参数</param>
        /// <param name="pageArgs">分页参数</param>
        /// <param name="modelOrder">排序参数</param>
        List<T> Search(SearchArgs searchArgs, PageArgs pageArgs, ResultOrder modelOrder);

        /// <summary>
        ///  分页查询对象列表
        /// </summary>
        /// <param name="searchArgs">查询参数</param>
        /// <param name="pageArgs">分页参数</param>
        /// <param name="modelOrder">排序参数</param>
        /// <param name="resultColumn">需要查询的属性名称列表(为空时，表示查询所有)</param>
        /// <returns></returns>
        List<T> Search(SearchArgs searchArgs, PageArgs pageArgs, ResultOrder modelOrder, ResultColumn resultColumn);
    }
}
